<?php
/** 
* blogcontroller.php
* Create on 2015-09-09 09:57:38
* Create by yuebin
*/
namespace haibao\cms\data\control;


class BlogController extends \haibao\cms\data\BaseMysql {
	
	const ORDER_BY_DATE = 'dateline';
	const ORDER_BY_VIRE = 'viewnum_false';
	
	const COUNT_TYPE_CATE_ID = 1;
	const COUNT_TYPE_UID = 2; 
	
	const PAGE_NUM = 10;
	public function __construct() {
		parent::__construct ( '\haibao\cms\model\data\Base' );
		$this->setConfig( \haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_MYSQL_HAIBAO_DZ3 );
	}
	
	/**
	 * 获取分类下所有的用户
	 */
	public function getUserListByCateId($cateId){
		$userList = array();
		if(!$cateId) return $userList;
		$sql = 'SELECT * FROM `hb_zj_blogs_cate_user` where cate_id=%s;';
		$result = $this->query($sql,array($cateId));
		while($result->fetch_assoc()){
			$temp = array();
			$temp['uid'] = $result->getData('uid');
			$temp['uname'] = $result->getData('uname');
			$userList[$result->getData('uid')] = $temp;
		}
		return $userList;
	}
	
	/**
	 * 根据用户ID获取资讯
	 */
	public function getBlogListByUserId($userId,$limit,$orderBy,$page){
		$blogList = array();
		if(!$userId)  return $blogList;
		$sql = 'SELECT * FROM `hb_home_blog`';
		if(is_array($userId)){
			$userIdStr = implode(',', $userId);
			$str = preg_replace('/(\d+)/', '%s', $userIdStr);
			$sql .= ' WHERE uid in ('.$str.') and status=0';
		}else{
			$sql .= ' WHERE uid=%s and status=0';
			$userId = (array)$userId;
		}
		if($orderBy == self::ORDER_BY_DATE){
			$sql .= ' ORDER BY '.self::ORDER_BY_DATE.' DESC';
		}elseif($orderBy == self::ORDER_BY_VIRE){
			$sql .= ' ORDER BY '.self::ORDER_BY_VIRE.' DESC';
		}
		$sql .= ' LIMIT '.($page-1)*$limit.','.$limit;
		$result = $this->query($sql,$userId);
		
		while ($result->fetch_assoc()){
			$temp = array();
			$temp['blogid'] = $result->getData('blogid');
			$temp['subject'] = $result->getData('subject');
			$temp['uid'] = $result->getData('uid');
			$temp['username'] = $result->getData('username');
			$temp['dateline'] = $result->getData('dateline');
			$blogList[$result->getData('blogid')] = $temp;
		}
		return $blogList;
	}
	
	/**
	 * 根据博主资讯ID获取导读图
	 */
	public function getBlogIntroPic($blogId){
		$introPicList = $blogPicArr = array();
		if(!$blogId)  return $introPicList;
		
		$sql = 'select filename,blogid from hb_zj_blogs_pic';
		if(is_array($blogId)){
			$blogIdStr = implode(',', $blogId);
			$str = preg_replace('/(\d+)/', '%s', $blogIdStr);
			$sql .= ' WHERE blogid in ('.$str.') and disrel=1';
		}else{
			$sql .= ' WHERE blogid=%s and disrel=1 limit 1';
			$blogId = (array)$blogId;
		}
		$picResult = $this->query($sql,$blogId);
		while($picResult->fetch_assoc()){
			$introPicList[$picResult->getData('blogid')] = $picResult->getData('filename');
		}
		return $introPicList;
	}
	
	/**
	 * 根据博主资讯ID获取资讯信息
	 */
	public function getBlogInfoByBlogId($blogId,$isGetMessage=true){

		$blogInfo = null;
		if(!$blogId)  return $blogInfo;
		$sql = 'SELECT
					home_blog.`blogid`,
					home_blog.`subject`,
					home_blog.`uid`,
					home_blog.`username`,
					home_blog.`subject_z`,
					home_blog.`dateline`,
					home_blog.`comments_count`,
					home_blog.`comments_parise_count`,
		            home_blog.`question_count`,
					home_blog.`question_parise_count`';
		if($isGetMessage) $sql .= ',blog_content.`message`';
		$sql .= 'FROM `hb_home_blog` AS home_blog';

		if($isGetMessage) $sql .= ' INNER JOIN `hb_home_blogfield` AS blog_content ON home_blog.blogid = blog_content.blogid';
		$sql .= ' WHERE home_blog.blogid = %s limit 1';
		$blogInfo = $this->query($sql,array($blogId))->fetch_assoc();
		return $blogInfo;
	}
	
	/**
	 * 获取博主用户信息
	 */
	public function getBloggerInfoById($bloggerId,$isGetAll = true){

		$bloggerInfo = array();
		if(!$bloggerId)  return $bloggerInfo;

		$sql = 'SELECT cate_id,uid,uname FROM `hb_zj_blogs_cate_user` where uid=%s';
		$bloggerInfo = $this->query($sql,array((int)$bloggerId))->fetch_assoc();
		if(empty($bloggerInfo)) return array();
		
		if($isGetAll){
			$sql = "SELECT  blogs,following FROM `hb_common_member_count` WHERE `uid`=%s";
			$blogCount = $this->query($sql,array((int)$bloggerId))->fetch_assoc();
			$bloggerInfo['blogCount'] = $blogCount['blogs']?$blogCount['blogs']:0;
			$bloggerInfo['fansCount'] = $blogCount['following']?$blogCount['following']:0;
			
			$sql = 'select count(*) as cnt from hb_home_album where `uid`=%s' ;
			$albumCount = $this->query($sql,array((int)$bloggerId))->fetch_assoc();
			$bloggerInfo['albumCount'] = $albumCount['cnt'];
			
			$this->setConfig( \haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_MYSQL_HAIBAO_USER );
			$sql = 'SELECT `Sign` FROM `UserProfiles` WHERE `UserId`=%s';
			$bloggerProfile = $this->query($sql,array((int)$bloggerId))->fetch_assoc();
			$bloggerInfo['bloggerProfile'] = $bloggerProfile['Sign']?$bloggerProfile['Sign']:'海报时尚网博主，个人资料陆续更新中，请保持关注！';
			$this->setConfig( \haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_MYSQL_HAIBAO_DZ3 );
		}
		return $bloggerInfo;
	}
	
	/**
	 * 按照时间排序获取博客列表
	 * @cateId   分类id
	 * @page     页码
	 */
	public function getBlogListByCateId($cateId,$page,$orderBy = self::ORDER_BY_DATE){
		$blogList = array();
		$tag = 0;
		$whereAndStr = '';
		if($orderBy == self::ORDER_BY_VIRE){
			$monthAgo = strtotime('last month');
			$whereAndStr = " and a.dateline > ".$monthAgo." ";
		}
		$sql = 'select
					* FROM `hb_home_blog` as a 
					left JOIN `hb_zj_blogs_cate_user` as b 
					ON a.uid = b.uid
					where b.cate_id = %s and a.status=0 '.$whereAndStr.'
					order by a.'.$orderBy.' desc LIMIT %s,10';
		$result = $this->query($sql,array($cateId,($page-1)*10));
		while($result->fetch_assoc()){
			$blogList[$tag]['BlogId'] = $result->getData('blogid');
			$blogList[$tag]['UserName'] = $result->getData('username');
			$blogList[$tag]['Uid'] = $result->getData('uid');
			$blogList[$tag]['Title'] = $result->getData('subject');
			$blogList[$tag]['CreateTime'] = date("Y-m-d H:i:s",$result->getData('dateline'));
			$blogList[$tag]['ViewNum'] = $result->getData('viewnum_false');
			$tag++;
		}
		return $blogList;
	}
	
	/**
	 * 取得数据的总条数
	 */
	public function getResultCountByObjectId($objectId,$type = self::COUNT_TYPE_CATE_ID){
		if($type == self::COUNT_TYPE_CATE_ID){
			$sql = 'select
					 count(*) as cnt from `hb_home_blog` as a
					left join `hb_zj_blogs_cate_user` as b
					ON a.uid = b.uid
					where b.cate_id = %s and a.status=0
					';
		}else{
			$sql = 'select
					 count(*) as cnt from `hb_home_blog` 
					where uid = %s and status=0';
		}
		$result = $this->query($sql,array($objectId));
		while($result->fetch_assoc()){
			return $result->getData('cnt');
		}
		
	}
	
	/**
	 * 获取热门标签
	 */
	public function getBlogHotTags($blogId){
		$hotTagArr = $tagIds = array();
		$sql = "SELECT `tagid` FROM `hb_common_tagitem`  WHERE `itemid`=%s and `idtype`='blogid' GROUP BY `tagid` ORDER BY count(`tagid`) DESC LIMIT 5";
		$result = $this->query($sql,array($blogId));
		while($result->fetch_assoc()){
			array_push($tagIds,$result->getData('tagid'));
		}
		
		if(!$tagIds) return $hotTagArr;
		
		$tagidsStr = preg_replace('/(\d+)/', '%s', implode(",",$tagIds));
		$sql = "SELECT `tagid`,`tagname` FROM `hb_common_tag` WHERE `tagid` IN(".$tagidsStr.") AND `status`=0";
		$tagList = $this->query($sql,$tagIds);
		while($tagList->fetch_assoc()){
			$temp = array();
			$temp['tagid'] = $tagList->getData('tagid');
			$temp['tagname'] = $tagList->getData('tagname');
			array_push($hotTagArr,$temp);
		}
		return $hotTagArr;
	}
	
	/**
	 * 博主文章添加点击数
	 */
	public function addBlogClickNum($blogId){
		$sql = 'update hb_home_blog set viewnum=viewnum+1,viewnum_false=viewnum_false+1 where blogid=%s';
		$this->query($sql,array($blogId));
	}

	/**
	 * 更新博主文章评论数
	 */
	public function updateCommentNum($blogId,$num=1,$isSub=false){
		if($isSub){
			$sql = "update hb_home_blog set comments_count=comments_count-".$num." where blogid=%s";
		}else{
			$sql = "update hb_home_blog set comments_count=comments_count+".$num." where blogid=%s";
		}
		$this->query($sql,array($blogId));
	}
	/**
	 * 根据博主资讯ID获取资讯信息
	 */
	public function getBlogInfoByBlogIds($blogIds){
	
		$blogList = array();
		if(!$blogIds)  return $blogList;
		$str = preg_replace('/\d+/', '%s',implode(',',$blogIds));
		$sql = 'SELECT blogid,subject FROM `hb_home_blog` WHERE blogid in ('.$str.') and status=0';
		$result = $this->query($sql,$blogIds);
		while ($result->fetch_assoc()){
			$temp = array();
			$temp['blogid'] = $result->getData('blogid');
			$temp['subject'] = $result->getData('subject');
			$blogList[$result->getData('blogid')] = $temp;
		}
		return $blogList;
	}
}